package org.yx.service.sres.save_spot_check;

import org.yx.exception.BizException;
import org.yx.models.SresSptChk;
import org.yx.utils.BaseService;
import org.yx.utils.FieldMap;

/**
 * 保存点检项目（新增、修改或删除）
 * 
 * @author zxw
 * @since 2019/09/04
 **/
public class SresSaveSpotCheckService extends BaseService {
	public SresSaveSpotCheckOut saveSpotCheck(SresSaveSpotCheckIn in) throws Exception {
		if (in.getStatus() != null && in.getStatus() == 'D') {
			checkStringNotBlank(in, "checkCode");
		} else {
			checkStringNotBlank(in, "checkCode,checkName");
		}

		SresSptChk chk = new SresSptChk();
		copyProperties(in, chk);
		chk.setOrgCode(in.getUserOrgCode());
		if (chk.getStatus() == null) {
			chk.setStatus('N');
		}

		if ('N' == chk.getStatus()) {
			insert(chk);
		} else {
			SresSptChk oldChk = selectOneLocked(SresSptChk.class, chk);
			if (oldChk == null || oldChk.getStatus() == 'D') {
				throw new BizException("SRES-0003", "点检项目${checkCode}不存在或已删除。", FieldMap.of("checkCode",
						chk.getCheckCode()));
			}

			if (chk.getStatus() == 'D') {
				update(chk, "status");
			} else {
				update(chk);
			}
		}

		SresSaveSpotCheckOut out = new SresSaveSpotCheckOut();

		return out;
	}
}